package com.bw.service.impl;

import com.bw.dao.PurchaseDao;
import com.bw.dao.impl.PurchaseDaoImpl;
import com.bw.pojo.PurchaseGoods;
import com.bw.pojo.vo.PurchaseVo;
import com.bw.service.PurchaseGoodsService;
import com.bw.util.JDBCUtil;
import com.bw.util.MyBatisUtil;
import com.bw.util.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.xml.internal.bind.v2.model.core.MaybeElement;
import lombok.SneakyThrows;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PurchaseGoodsServiceImpl implements PurchaseGoodsService {
    SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
    PurchaseDaoImpl dao = new PurchaseDaoImpl();
    @Override
    public PageInfo<PurchaseVo> getPurchaseList(PurchaseVo purchaseVo) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PurchaseDao mapper = sqlSession.getMapper(PurchaseDao.class);
        PageHelper.startPage(purchaseVo.getPageNum(),purchaseVo.getPageSize());
        List<PurchaseVo> list = mapper.getPurchaseList(purchaseVo);
        PageInfo<PurchaseVo> purchaseVoPageInfo = new PageInfo<>(list);
        sqlSession.close();
        return purchaseVoPageInfo;
    }



    @SneakyThrows
    @Override
    public void addPurchase(PurchaseVo purchaseVo) {
        //多对多的添加业务流程 3步
        //1.给进货单据表 添加一条测试数据，并获取主键id的值
        //2.循环集合，给中间表添加数据
        //3.更新库存 （进货，采购 +库存，退货，出库 -库存）

        //事务 ：要么同时成功，要么同时失败
        //jdbc事务
        //1.获取连接 JDBCUtil.getConnection(); 获取返回值对象
        //2.关闭自动 开启手动 conn.setAutoCommit(false)
        //3.执行多对多的业务流程（3步）
            //3.1.给进货单据表 添加一条测试数据，并获取主键id的值
            //3.2.循环集合，给中间表添加数据
            //3.3.更新库存 （进货，采购 +库存，退货，出库 -库存）
        //4.如果没有异常，提交事务 conn.commit();
        //5.报错 捕捉异常catch 进行事务回滚 conn.rollback()
        //6.finally 中 1.关闭 手动事务，恢复自动 conn.setAutoCommit(true);
//                    2.关闭连接 conn.close();
        //-----------------------------------------------------------------------------
        Connection conn = null;
        try {
            //1.获取连接
            conn = JDBCUtil.getConn();
            //2.关闭自动 开启手动
            conn.setAutoCommit(false);
            //执行多对多的业务流程
            //1.给进货单据表 添加一条测试数据 获取主键id
            int purchaseId = dao.addPurchase(conn,purchaseVo);
            //2.循环集合 给中间表添加数据
            ArrayList<PurchaseGoods> arr = purchaseVo.getArr();
            for (PurchaseGoods purGoods : arr) {
                //给中间表数据（进货id，商品id，进货数量，进货金额）
                dao.addPurGoods(conn,purchaseId,purGoods.getGoodsId(),purGoods.getNum(),purGoods.getMoney());
                //3.更新库存，循环（update t_goods set 库存=库存+入库数量 where id = ?）
                dao.updGoods(conn,purGoods.getNum(),purGoods.getGoodsId());
            }
            //提交事务 !!!
            conn.commit();
        } catch (Exception e) {
            //报错 回滚
            conn.rollback();
            //千万给我记住了，没他出不来的
            e.printStackTrace();;
        } finally {
            //1.先回复自动提交
            conn.setAutoCommit(true);
            //2.关闭
            conn.close();
        }


    }

    @SneakyThrows
    @Override
    public void addPurchases(PurchaseVo purchaseVo) {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConn();
            conn.setAutoCommit(false);
           Integer purchaseId = dao.addPurchases(conn,purchaseVo);
            ArrayList<PurchaseGoods> arr = purchaseVo.getArr();
            for (PurchaseGoods purchaseGoods : arr) {
                dao.getAddPurchase(conn,purchaseId,purchaseGoods.getGoodsId(),purchaseGoods.getNum(),purchaseGoods.getMoney());
                dao.getUpdGoods(conn,purchaseGoods.getNum(),purchaseGoods.getGoodsId());
            }
        } catch (Exception e) {
            conn.rollback();
            e.printStackTrace();
        } finally {
            conn.setAutoCommit(true);
            conn.close();
        }


    }
}
